package com.sunwayworld.basemodule.coredata.test.validators;

import com.sunwayworld.basemodule.coredata.experimentaltemplate.bean.LimsExperimentalTemplateTestBean;
import com.sunwayworld.basemodule.coredata.experimentaltemplate.service.LimsExperimentalTemplateTestService;
import com.sunwayworld.basemodule.coredata.test.bean.LimsTestBean;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.validator.data.DataValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsTestDeleteTableValidator implements DataValidator {

    // 需要关联的service
    @Autowired
    private LimsExperimentalTemplateTestService spTestService;

    @Override
    public boolean doValid(Object... args) {
        RestJsonWrapperBean wrapper = (RestJsonWrapperBean) args[0];
        boolean result = true;
        List<LimsTestBean> testList = wrapper.parse(LimsTestBean.class);
        List<Long> idList = testList.stream().map(LimsTestBean::getId).collect(Collectors.toList());

        // 查询被引用的数据
        SearchFilter filter = SearchFilter.instance().match("testId", idList).filter(MatchPattern.OR);

        List<LimsExperimentalTemplateTestBean> beans = spTestService.selectListByFilter(filter);
        if (!beans.isEmpty()) {
            this.addConstraintViolation(I18nHelper.getMessage("MODULE.COREDATA.TEST.TIP.DELETE_NOT"));
            result = false;
        }
        return result;
    }
}
